package com.southwind.controller;


import com.southwind.entity.ProductInfo;
import com.southwind.service.IProductInfoService;
import com.southwind.vo.ProductInfoPageVO;
import com.southwind.vo.ProductInfoVO;
import com.southwind.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 商品表 前端控制器 卖家端
 * </p>
 *
 * @author admin
 * @since 2021-04-18
 */
@RestController
@RequestMapping("/seller/product")
public class ProductInfoSellerController {

    @Autowired
    private IProductInfoService productInfoService;

    @GetMapping("/list/{page}/{size}")
    public ResultVO<ProductInfoPageVO> list(@PathVariable Long page, @PathVariable Long size) {
        ProductInfoPageVO productInfoPageVO = productInfoService.productInfoPageVO(page, size);
        return ResultVO.successWithData(productInfoPageVO);
    }

    @GetMapping("/findById/{id}")
    public ResultVO<ProductInfoVO> list(@PathVariable Integer id) {
        ProductInfoVO productInfoVO = productInfoService.findById(id);
        return ResultVO.successWithData(productInfoVO);
    }

    @DeleteMapping("/delete/{id}")
    public ResultVO delete(@PathVariable Integer id) {
        boolean b = productInfoService.removeById(id);
        return ResultVO.auto(b);
    }

    @PutMapping("/updateStatus/{id}/{status}")
    public ResultVO updateStatus(@PathVariable Integer id, @PathVariable Boolean status) {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProductId(id);
        productInfo.setProductStatus(status ? 1 : 0);
        boolean b = productInfoService.updateById(productInfo);
        return ResultVO.successWithData(status);
    }

    @PutMapping("/update")
    public ResultVO update(@RequestBody ProductInfoVO productInfoVO) {
        ProductInfo productInfo = new ProductInfo();
        BeanUtils.copyProperties(productInfoVO, productInfo);
        productInfo.setProductStatus(productInfoVO.getStatus() ? 1 : 0);
        productInfo.setCategoryType(productInfoVO.getCategory().getCategoryType());
        boolean b = productInfoService.updateById(productInfo);
        return ResultVO.auto(b);
    }
}

